﻿Imports System.Drawing
Imports GTA
Imports GTA.Math
Imports GTA.Native
Imports LemonUI
Imports LemonUI.Elements
Imports LemonUI.Menus

Public Class ElimilateMissionScript
    Inherits Script
    Private Shared currentMission As Mission
    Public Sub New()
        Interval = 500
    End Sub
    Public Shared Sub AbortCurrentMission()
        currentMission?.Abort()
        currentMission = Nothing
    End Sub
    Public Shared Sub StartNewMission(mission As Mission)
        If currentMission IsNot Nothing Then
            If currentMission.IsCompleted Then
                Game.IsMissionActive = True
                Game.MaxWantedLevel = 0
                Game.Player.WantedLevel = 0
                currentMission = mission
                currentMission.Init()
            Else
                UI.Notification.PostTicker("在完成上一个任务的前无法开始新任务", False)
            End If
        Else
            Game.IsMissionActive = True
            Game.MaxWantedLevel = 0
            Game.Player.WantedLevel = 0
            currentMission = mission
            currentMission.Init()
        End If
    End Sub
    Private Sub ElimilateMissionScript_Tick(sender As Object, e As EventArgs) Handles Me.Tick
        If currentMission IsNot Nothing Then
            currentMission.DoTick()
        End If
    End Sub
    Public Class Mission
        Private ReadOnly destination As Vector3
        Private destinationBlip As Blip
        Private destinationText As String
        Private ReadOnly enermies As EntityCollection(Of Ped) = New EntityCollection(Of Ped)()
        Private state As MissionState
        Private centerPosition As Vector3
        Private ReadOnly enermyModels As Model()
        Private ReadOnly enermyWeapons As WeaponHash()
        Private generatedEnermies As Integer = 0
        Private ReadOnly maxEnermies As Integer = Pick(20, 7)
        Public Sub New(startPoint As Vector3, enermyModels As Model(), enermyWeapons As WeaponHash())
            destination = startPoint 'World.GetNextPositionOnStreet(startPoint.Around(40))
            While destination.DistanceTo(startPoint) < 40
                destination = World.GetNextPositionOnStreet(startPoint.Around(50))
                Script.Wait(100)
            End While
            centerPosition = startPoint
            Me.enermyModels = enermyModels
            Me.enermyWeapons = enermyWeapons
            state = MissionState.GoToDestination
        End Sub
        Public Sub Init()
            destinationBlip = World.CreateBlip(destination)
            destinationBlip.ShowRoute = True
            destinationText = World.GetStreetName(Me.destination)
        End Sub
        Public ReadOnly Property IsCompleted As Boolean
            Get
                If generatedEnermies >= maxEnermies AndAlso enermies.Count = 0 Then
                    Return True
                Else
                    Return False
                End If
            End Get
        End Property
        Public Function GenerateEnermy(center_position As Vector3, model As Model, weapon As WeaponHash) As Boolean
            Dim distance As Single = Pick(10)
            Dim generationLocation As Vector3
            If World.GetSafePositionForPed(center_position.Around(distance), generationLocation, GetSafePositionFlags.OnlyNetworkSpawn) Then
                Dim enermy As Ped = World.CreatePed(model, generationLocation)
                If enermy Is Nothing Then
                    Return False
                End If
                With enermy.AddBlip()
                    .Sprite = BlipSprite.Enemy
                    .Scale = 0.7
                    .Color = BlipColor.Red
                End With
                enermy.SetCombatAttribute(CombatAttributes.PerfectAccuracy, True)
                enermy.CombatMovement = CombatMovement.WillRetreat
                enermy.Health = 500
                '[Function].Call(Hash.SET_PED_HAS_AI_BLIP, enermy, True)
                enermy.SetCombatAttribute(CombatAttributes.DisableInjuredOnGround, True)
                enermy.Weapons.Give(weapon, 150, True, True)
                enermy.RelationshipGroup = EnermyRelationshipGroup
                EntityManagement.AddController(New EnermyController(enermy, "敌人"))
                enermies.Add(enermy)
                enermy.Task.Combat(PlayerPed)
                Return True
            Else
                Return False
            End If
        End Function
        Public Sub DoTick()
            If state = MissionState.GoToDestination Then
                If PlayerPed.Position.DistanceTo2D(destination) > 10 Then
                    UI.Screen.ShowSubtitle("前往 ~y~" + destinationText)
                Else
                    destinationBlip.Delete()
                    state = MissionState.KillEnermies
                End If
            ElseIf state = MissionState.KillEnermies Then
                While generatedEnermies < maxEnermies
                    While Not GenerateEnermy(centerPosition, Pick(enermyModels), Pick(enermyWeapons))
                        Wait(1000)
                    End While
                    generatedEnermies += 1
                End While
                enermies.Keep(KeepRule.MustAlive)
                If Not IsCompleted Then
                    UI.Screen.ShowSubtitle($"消灭 ~r~敌人")
                    Game.MaxWantedLevel = 0
                    Game.Player.WantedLevel = 0
                Else
                    Dim x As Integer = UI.Screen.MainWindowResolution.Width / 2
                    Dim frame As ScaledRectangle = New ScaledRectangle(New PointF(x - 1000, 335), New SizeF(2000, 250))
                    frame.Color = Color.FromArgb(140, 10, 10, 10)
                    Dim text As ScaledText = New ScaledText(New PointF(x, 350), "任务完成")
                    text.Color = Color.DarkGoldenrod
                    text.Alignment = UI.Alignment.Center
                    text.Font = UI.Font.Pricedown
                    text.Scale = 2
                    Dim s As Sound = New Sound("REMOTE_PLYR_CASH_COUNTER_COMPLETE", "DLC_HEISTS_GENERAL_FRONTEND_SOUNDS")
                    s.PlayFrontend()
                    Dim timer As Stopwatch = New Stopwatch()
                    timer.Start()
                    While True
                        If timer.Elapsed.TotalSeconds > 4 Then
                            Exit While
                        End If
                        Script.Wait(1)
                        frame.Draw()
                        text.Draw()
                    End While
                    state = MissionState.Abort
                    Game.IsMissionActive = False
                    Game.MaxWantedLevel = 5
                End If
            End If
        End Sub
        Public Sub Abort()
            state = MissionState.Abort
            destinationBlip.Delete()
            For Each enermy As Ped In enermies
                enermy.Delete()
            Next
        End Sub
        Public Sub Start()
            StartNewMission(Me)
        End Sub
    End Class
    Public Enum MissionState
        Abort = -1
        GoToDestination = 0
        KillEnermies = 1
    End Enum
End Class
